package it.polimi.serviceTech.email;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.github.sendgrid.SendGrid;

public class EmailService extends HttpServlet{
	// Constants
	private static final long serialVersionUID = 1L;
	private static String from = "admin@tivendoio.it";
	private static String subject = "Confirm your TiVendoIo account!";
	private static String body = "<h1>Hi, please confirm your account on TiVendoIo.it by simply clicking"
			+ " on the following link. </h1></br>";
	private static String footer = "<h1>Thank you, the staff.</h1>";
	private static String username = "dylaniato";
	private static String password = "deni1990";
	
	// Local variables
	private String link = "";

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		// Get parameters
		String toEmail = request.getParameter("email");
		String idString = request.getParameter("id");
		link = "<a href=\"localhost:8080/ISellToYouRESTWebService/ActivateUser?userId="+idString+"\">CLICK</a>";
		
		// Setting email parameters
		SendGrid sendgrid = new SendGrid(username, password);
		sendgrid.addTo(toEmail);
		sendgrid.setFrom(from);
		sendgrid.setSubject(subject);
		sendgrid.setHtml(body + link + footer);
		
		// Sending email
		sendgrid.send();
		
		System.out.println("Mail sent!");
	}
	
}
